package com.woniuxy.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.Area;
import com.woniuxy.entity.Material;
import com.woniuxy.mapper.MaterialMapper;
import com.woniuxy.service.MaterialService;

@Service
public class MaterialServiceImpl implements MaterialService{
	@Autowired
	private MaterialMapper materialMapper;

	@Override
	public PageDto<Material> findAllMaterial(int page) {
		PageDto<Material> pageDto;
		synchronized (MaterialServiceImpl.class) { //加锁
			PageHelper.startPage(page, 10);
			List<Material> materials = materialMapper.findAllMaterial();
			PageInfo<Material> pageInfo = new PageInfo<>(materials);
			pageDto = new PageDto<Material>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(materials);
		}		
		return pageDto;	
	}

	@Override
	public int addMaterial(Material material) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String resDate = dateFormat.format(date);
		material.setTime(resDate);
		return materialMapper.addMaterial(material);
	}

	@Override
	public PageDto<Material> findMaterialByStatus(int status, int page) {
		PageDto<Material> pageDto;
		synchronized (MaterialServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Material> materials = materialMapper.findMaterialByStatus(status);
			PageInfo<Material> pageInfo = new PageInfo<>(materials);
			pageDto = new PageDto<Material>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(materials);
		}
		return pageDto;
	}

	@Override
	public PageDto<Material> findMaterialByName(String name, int page) {
		PageDto<Material> pageDto;
		synchronized (MaterialServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Material> materials = materialMapper.findMaterialByName(name);
			PageInfo<Material> pageInfo = new PageInfo<>(materials);
			pageDto = new PageDto<Material>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(materials);
		}
		return pageDto;
	}

	@Override
	public int deleteMaterialByMaid(int maid) {
		return materialMapper.delectMaterialByMaid(maid);
	}

	@Override
	public int updateSwitch(int maid, int value) {
		int data;
		synchronized(MaterialServiceImpl.class) {
			Material material = new Material().setMaid(maid).setStatus(value);
			data = materialMapper.updateSwitch(material);
		}
		return data;
	}

}
